using System;
using System.Collections.Generic;
using System.Text;

namespace Placid.Core
{
    public class PTreeNodesMeasuresCollection : IDisposable
    {
        private Dictionary<string, PTreeNodesMeasure> _nodesMs;

        public PTreeNodesMeasuresCollection()
        {
            _nodesMs = new Dictionary<string, PTreeNodesMeasure>();
        }

        public PTreeNodesMeasure AddNodesMeasure(PTreeNode hNode, PTreeNode vNode, PTreeNodesMeasure.MeasureType type)
        {
            PTreeNodesMeasure nodesM = null;

            if (hNode != null && vNode != null)
            {
                nodesM = GetNodesMeasure(hNode, vNode);
                if (nodesM == null || nodesM == PTreeNodesMeasure.Empty)
                {
                    nodesM = new PTreeNodesMeasure(hNode, vNode, type);
                    _nodesMs.Add(hNode.Path + "!" + vNode.Path, nodesM);
                }

                return nodesM;
            }

            return PTreeNodesMeasure.Empty;
        }

        public PTreeNodesMeasure GetNodesMeasure(PTreeNode hNode, PTreeNode vNode)
        {
            if (hNode != null && vNode != null && _nodesMs.ContainsKey(hNode.Path + "!" + vNode.Path))
            {
                return _nodesMs[hNode.Path + "!" + vNode.Path];
            }
            else
            {
                return PTreeNodesMeasure.Empty;
            }
        }


        #region IDisposable Members

        public void Dispose()
        {
            foreach (PTreeNodesMeasure nm in _nodesMs.Values) nm.Dispose();
            _nodesMs.Clear();
        }

        #endregion
    }
}
